package com.enation.app.shop.front.api.credit.service.impl;

import com.enation.app.shop.front.api.credit.entity.CreditAccount;
import com.enation.app.shop.front.api.credit.service.ICreditAccountManager;
import com.enation.framework.database.IDaoSupport;
import com.enation.framework.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("creditAccountService")
public class CreditAccountManager implements ICreditAccountManager {
    @Autowired
    private IDaoSupport daoSupport;
    @Override
    public CreditAccount getCreditByUserId(int userid) {
        String sql="select * from rbt_credit_account where user_id=?";
        List<CreditAccount> creditAccountList1=daoSupport.queryForList(sql,CreditAccount.class,userid);
        if (creditAccountList1.size()==0){
            return null;
        }else {
            return  creditAccountList1.get(0);
        }
    }

    public  CreditAccount getCreditByid(int creditid){
        String sql="select * from rbt_credit_account where credit_id=?";
        List<CreditAccount> creditAccountList1=daoSupport.queryForList(sql,CreditAccount.class,creditid);
        if (creditAccountList1.size()==0){
            return null;
        }else {
            return  creditAccountList1.get(0);
        }
    };

    @Override
    public int createCredit(CreditAccount creditAccount) {

        this.daoSupport.insert("rbt_credit_account",creditAccount);
//        int creditid = this.daoSupport.getLastId("rbt_credit_account");
//        if (creditid!=0){
//            return   creditid;
//        }else {
//            return  0;
//        }
        return 2;
    }

    @Override
    public int updateCredit(CreditAccount creditAccount) {

        this.daoSupport.update("rbt_credit_account", creditAccount, "user_id=" + creditAccount.getUser_id());

        return 1;
    }

    @Override
    public void updatepayPassword(String paypassword,int userId) {
        String md5password = paypassword == null ? StringUtil.md5("") : StringUtil.md5(paypassword);
        String sql = "update rbt_credit_account set payment_password = ? where user_id =? ";
        this.daoSupport.execute(sql, md5password, userId);
    }

    public  List<CreditAccount> getAll(){
        String sql="select * from rbt_credit_account";
        return this.daoSupport.queryForList(sql,CreditAccount.class);
    };
}
